<?php

/**
 * Class ProfilingLog
 * @property string $minProfilingTime
 * @property string $avgProfilingTime
 * @property string $maxProfilingTime
 */
class ProfilingLog extends BaseProfilingLog {
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 获取查询条件
     * @return CDbCriteria
     */
    public function getSearchCriteria() {

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('uri', $this->uri, true);
        $criteria->compare('controller', $this->controller);
        $criteria->compare('action', $this->action);
        if (is_array($this->logTime) && count($this->logTime) == 2) {
            $beginTime = isset($this->logTime['start']) ? $this->logTime['start'] : '';
            $endTime = isset($this->logTime['end']) ? $this->logTime['end'] : '';
            $criteria->addBetweenCondition('logTime', $beginTime, $endTime);
        } else {
            $criteria->compare('logTime', $this->logTime, true);
        }
        $criteria->compare('getParams', $this->getParams, true);
        $criteria->compare('postParams', $this->postParams, true);
        $criteria->compare('profilingLog', $this->profilingLog, true);
        $criteria->compare('profilingTime', $this->profilingTime);
        $criteria->order = 'id desc';
        return $criteria;
    }

    /**
     * 获取字段名
     * @return array
     */
    public function attributeLabels() {
        return array('id' => Yii::t('app', 'ID'), 'uri' => Yii::t('app', 'Uri'), 'controller' => Yii::t('app', 'Controller'), 'action' => Yii::t('app', 'Action'), 'logTime' => Yii::t('app', '记录时间'), 'getParams' => Yii::t('app', 'Get 参数'), 'postParams' => Yii::t('app', 'Post 参数'), 'profilingLog' => Yii::t('app', '耗时日志'), 'profilingTime' => Yii::t('app', '耗时时长'),);
    }

    /**
     * 获取查询DataProvider
     * @return CActiveDataProvider
     */
    public function search() {
        $criteria = $this->getSearchCriteria();

        return new CActiveDataProvider($this, array('criteria' => $criteria,));
    }

    /**
     * 获取最小请求耗时
     * @return float
     */
    public function getMinProfilingTime() {
        $criteria = $this->getSearchCriteria();
        $criteria->select = 'min(profilingTime) as profilingTime';
        $log = self::model()->find($criteria);
        /* @var $log ProfilingLog */
        return $log->profilingTime;
    }

    /**
     * 获取最大请求耗时
     * @return float
     */
    public function getMaxProfilingTime() {
        $criteria = $this->getSearchCriteria();
        $criteria->select = 'max(profilingTime) as profilingTime';
        $log = self::model()->find($criteria);
        /* @var $log ProfilingLog */
        return $log->profilingTime;
    }

    /**
     * 获取平均请求耗时
     * @return float
     */
    public function getAvgProfilingTime() {
        $criteria = $this->getSearchCriteria();
        $criteria->select = 'avg(profilingTime) as profilingTime';
        $log = self::model()->find($criteria);
        /* @var $log ProfilingLog */
        return $log->profilingTime;
    }

    /**
     * 获取控制器列表
     * @return array
     */
    public function getControllerList() {
        $list = $this->dbConnection->createCommand('select distinct controller from profiling_log')->queryAll();
        $result = array();
        foreach ($list as $row) {
            $result[] = $row['controller'];
        }
        return $result;
    }

    /**
     * 获取action列表
     * @return array
     */
    public function getActionList() {
        $list = $this->dbConnection->createCommand('select distinct action from profiling_log')->queryAll();
        $result = array();
        foreach ($list as $row) {
            $result[] = $row['action'];
        }
        return $result;
    }

}